package com.juyu.miliao.api.dao.redis.usercache;



import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.cache.GradeCache;
import com.juyu.miliao.api.cache.RedisKeyPrefix;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.domain.EnergyLevel;
import com.juyu.miliao.api.domain.Grade;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.dto.IntegralLevelAndTitleDTO;
import com.juyu.miliao.api.domain.vo.HostUserLevelVo;
import com.juyu.miliao.api.service.EnergyLevelService;
import com.juyu.miliao.api.service.IntegralLevelInfoService;
import com.juyu.miliao.api.util.GradeUtil;
import com.juyu.miliao.api.util.HostUserLevelUtil;
import com.juyu.miliao.common.util.SumAgeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 用户信息缓存
 *
 * @author xuanqicheng
 * @create 2018-18-1:10 PM
 */
@Component
public class UserInfoCache {

    public static final String USER_INFO_CACHE = RedisKeyPrefix.MI_LIAO_API_KEY+"UserInfoCache:";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private UserInfoDao userInfoDao;

    @Autowired
    private IntegralLevelInfoService integralLevelInfoService;

    @Autowired
    private EnergyLevelService energyLevelService;

    /**
     * 缓存用户信息2分钟
     *
     * @param userId
     */
    public void addUserInfoCache(Long userId) {

        UserCacheVo userCacheVo = setUserCacheVo(userId);

        addCache(userCacheVo);
    }

    /**
     * 获取用户缓存
     * @param userId
     * @return
     */
    public UserCacheVo getUserCacheInfo(Long userId) {

        String key = USER_INFO_CACHE + userId;

        String temp = stringRedisTemplate.opsForValue().get(key);

        if (!StringUtils.isEmpty(temp)) {

            UserCacheVo userCacheVo = JSONObject.parseObject(temp,UserCacheVo.class);

            return userCacheVo;
        }else{
            UserCacheVo userCacheVo = setUserCacheVo(userId);

            if (userCacheVo !=null) {

                addCache(userCacheVo);
            }
            return userCacheVo;
        }

//        UserCacheVo userCacheVo = setUserCacheVo(userId);

//        return userCacheVo;

    }

    /**
     * 删除缓存
     * @param userId
     */
    public void deleteUserCacheInfo(Long userId){
        String key = USER_INFO_CACHE+userId;
        stringRedisTemplate.delete(key);
    }

    /**
     * 添加缓存
     * @param userCacheVo
     */
    private void addCache( UserCacheVo userCacheVo) {

        String key = USER_INFO_CACHE + userCacheVo.getUserId();

        stringRedisTemplate.opsForValue().set(key, JSONObject.toJSONString(userCacheVo), 2, TimeUnit.MINUTES);
    }


    /**
     * 设置
     *
     * @param userId
     * @return
     */
    private UserCacheVo setUserCacheVo(Long userId) {



        User user = userInfoDao.getUserInfoById(userId);
        if(user!=null){
            UserCacheVo userCacheVo = new UserCacheVo();
            userCacheVo.setUserId(user.getUserId());

            userCacheVo.setNickName(user.getNickName());

            userCacheVo.setMGradeSum(user.getmGradeSum());
            // 魅力等级
            List<Grade> gradeList = GradeCache.INSTANCE.getGradeList(1);

            userCacheVo.setMGrade(GradeUtil.getGradeCount(gradeList, user.getmGradeSum()));

            userCacheVo.setVGradeSum(user.getvGradeSum());
            // 土豪等级
            gradeList = GradeCache.INSTANCE.getGradeList(2);

            userCacheVo.setVGrade(GradeUtil.getGradeCount(gradeList, user.getvGradeSum()));

            userCacheVo.setBirthday(user.getBirthday());
            // 生日转换成年龄
            if (user.getBirthday() != null) {
                userCacheVo.setAge(SumAgeUtil.getAgeByBirth(user.getBirthday()));

            } else {

                userCacheVo.setAge(0);

            }

            userCacheVo.setIcon(user.getIcon());
            userCacheVo.setIsHost(user.getIsHost());
            userCacheVo.setIsVip(user.getIsVip());
            userCacheVo.setSex(user.getSex());
            userCacheVo.setVipLevel(user.getVipLevel());

            IntegralLevelAndTitleDTO integralDTO = integralLevelInfoService.getUserIntegralLevel(user.getIntegralSum());
            HostUserLevelVo hostUserLevel = HostUserLevelUtil.getHostUserLevel(integralDTO);
            userCacheVo.setHostUserLevel(hostUserLevel);
            EnergyLevel energyLevel = energyLevelService.getUserEnergyLevel(user.getEnergySum());
            userCacheVo.setUserLevel(energyLevel);
            userCacheVo.setIsSvip(user.getIsSvip());
            userCacheVo.setIsPlatinumVip(user.getIsPlatinumVip());
            return userCacheVo;
        }else{
            return null;
        }

    }
}
